// ##############################
// ####  note:  bokeweilai data main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_common.h"
#include "bkwl_data.h"
#include "dev_queue.h"
#include "bkwl_protocol_jiwei.h"

bk_err_t bkwl_data_init()
{
	bk_err_t ret = BK_OK;
	ret = bkwl_data_sys_init();
	ret = bkwl_data_main_init();
	ret = bkwl_data_mqtt_init(DEVICE_NAME, PRODUCT_KEY);
	return ret;
}

bk_err_t bkwl_data_sys_init()
{
	bk_err_t ret = BK_OK;

	tprintf("bkwl_data_sys_init entry\n\n");
	// bkwl_main->camera.image_cur.buffer = (uint8_t *)(1920 * 1024 * 3);
	bkwl_main->camera.image_cur.buffer = NULL;
	bkwl_main->status.mqtt_connect = 0;
	tprintf("bkwl_data_sys_init exit\n\n");

	return ret;
}

bk_err_t bkwl_data_main_init()
{
	bk_err_t ret = BK_OK;

	tprintf("bkwl_data_main_init entry\n\n");
	ret = rtos_init_queue(&bkwl_main->queue.msg,
						  "queue.msg",
						  sizeof(bkwl_msg_t),
						  10);
	if (ret != BK_OK)
	{
		tprintf("%s, ceate blwl msg queue failed\n");
		goto error;
	}
	// bkwl_main->queue.msg = &main_msg_queue;
#if BKWL_IOT_DOORBELL_EVENT_UART_DOOR_HANDEL == 1

	ret = rtos_init_queue(&bkwl_main->queue.uart_door,
						  "queue.uart_door",
						  sizeof(bkwl_msg_t),
						  10);
	if (ret != BK_OK)
	{
		tprintf("%s, ceate blwl uart_door queue failed\n");
		goto error;
	}
//	bkwl_protocol_init_door_queue(bkwl_main->queue.uart_door);
#endif

	uint32_t size = BKWL_IOT_DOOR_UART_BUF_SIZE;
	bkwl_main->ringbuffer.uart_door.overwrite = 1;
	ringbuffer_init(&bkwl_main->ringbuffer.uart_door, size, 1);

	tprintf("bkwl_data_main_init exit\n\n");
error:
	return ret;
}

bk_err_t bkwl_data_mqtt_init(char *device_name, char *product_key)
{
	bk_err_t ret = BK_OK;
	tprintf("bkwl_data_sys_init device_name=%s,product_key=%s\n\n", device_name, product_key);
	sprintf(bkwl_main->topic.MQTT_PROPERTY_EVENT_POST, "device/%s/%s/event/property/post", product_key, device_name);
	sprintf(bkwl_main->topic.MQTT_PROPERTY_SERVICE_SET, "device/%s/%s/service/property/set", product_key, device_name);
	sprintf(bkwl_main->topic.MQTT_NOTIFY_EVENT_POST, "device/%s/%s/event/notify/post", product_key, device_name);
	sprintf(bkwl_main->topic.MQTT_NOTIFY_EVENT_POST_REPLY, "device/%s/%s/event/notify/post_reply", product_key, device_name);
	sprintf(bkwl_main->topic.MQTT_SERVICE_GET, "device/%s/%s/service/%d", product_key, device_name, 1);
	sprintf(bkwl_main->topic.MQTT_SERVICE_GET_REPLY, "device/%s/%s/service/%d_reply", product_key, device_name, 1);

	return ret;
}
